const router = require('express').Router()
const U = { ...require('../../utils'), ...require('./utils') }
const M = require('kobako-maybe')
const Food = require('../../models/food')

function maybe_food(body){

  const name = body.name,
        desc = body.desc,
        price = body.price

  if(name == null || desc == null || price == null){
    return M.nothing()
  }

  return M.just({name,desc,price})
}

function add_food(shop_id,food){
  Food.add(shop_id,food)
}

router.route('/')
  .all(U.abort_not_login)
  .get((req, res) => {
    console.log('取出店家' + req.session.shop.shop + '的所有菜品信息')
    res.json(U.Ok(Food.all_foods_by_shop(req.session.shop.id)))
  })
  .post((req,res) => {
    const food = maybe_food(req.body)
    if(M.isNothing(food)){
      res.json(U.Err('请输入正确的菜品信息'))
      return 
    }
    add_food(req.session.shop.id,M.value(food))
    res.json(U.Ok())
  })

module.exports = router